package com.efast.cafe.portal.service.company.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.efast.cafe.framework.jdbc.JdbcDao;
import com.efast.cafe.framework.jdbc.JdbcDaoImpl;
import com.efast.cafe.framework.jdbc.persistence.Criteria;
import com.efast.cafe.framework.service.CafeServiceImpl;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.portal.dao.company.PortalCompanyRoleDao;
import com.efast.cafe.portal.entity.company.PortalCompanyPositionRole;
import com.efast.cafe.portal.entity.company.PortalCompanyRole;
import com.efast.cafe.portal.service.company.IPortalCompanyPositionRoleService;

/******************************************************************************
 * @File name : PortalCompanyPositionRoleServiceImpl.java
 * 
 * @Author : xl
 * 
 * @Date : 2016-8-25
 * 
 * @Copyright Notice: Copyright (c) 2016 Efast Serv. All Rights Reserved. This
 *            software is published under the terms of the Efast Software
 *            License version 1.0, a copy of which has been included with this
 *            distribution in the LICENSE.txt file.
 * 
 * 
 *            ------------------------------------------------------------------
 *            ---------- Date Who Version Comments 2016-8-25 下午5:25:19 xl 1.0
 *            Initial Version
 *****************************************************************************/
@Service
@Transactional
public class PortalCompanyPositionRoleServiceImpl extends CafeServiceImpl<PortalCompanyPositionRole> implements IPortalCompanyPositionRoleService {

	
	private PortalCompanyRoleDao portalCompanyRoleDao;
	
	@Qualifier("jdbcDao")
	@Autowired
	private  JdbcDao  jdbcDao;
	
	
	
	@Autowired
	public void setPortalCompanyRoleDao(PortalCompanyRoleDao portalCompanyRoleDao) {
		this.portalCompanyRoleDao = portalCompanyRoleDao;
	}

	

	/**
	 * {@inheritDoc} overridden:
	 * 
	 * @Date : 2016-8-25
	 * @see org.eae.eldf.portal.remote.company.PortalCompanyPositionRoleService#getPositionRoleMultiple(long)
	 **/
	@Override
	public Map<String, List<PortalCompanyRole>> getPositionRoleMultiple(Long positionId) {
		Map<String, List<PortalCompanyRole>> data = new HashMap<String, List<PortalCompanyRole>>();
		List<PortalCompanyRole> roleHasSelectList = portalCompanyRoleDao.getroleHasSelectList(positionId,SpringWebUtils.getRequestCompany());

		List<PortalCompanyRole> roleNotHasSelectList = portalCompanyRoleDao.getroleNotHasSelectList(roleHasSelectList,SpringWebUtils.getRequestCompany());
		data.put("roleHasSelectList", roleHasSelectList);
		data.put("roleNotHasSelectList", roleNotHasSelectList);
		return data;
	}

	/**
	 * {@inheritDoc} overridden:
	 * 
	 * @Date : 2016-8-25
	 * @see org.eae.eldf.portal.remote.company.PortalCompanyPositionRoleService#savePositionRoleMultiple(long,
	 *      java.lang.String[])
	 **/
	@Override
	public String savePositionRoleMultiple(Long positionId, Long[] roles) {
		Criteria delPositionRole = Criteria.create(PortalCompanyPositionRole.class).where(
				PortalCompanyPositionRole.portalCompanyPositionId, new Object[] { positionId });
		jdbcDao.delete(delPositionRole);

		for (Long roleId : roles) {
			Criteria positionRole = Criteria.create(PortalCompanyPositionRole.class)
					.set(PortalCompanyPositionRole.portalCompanyPositionId, positionId)
					.set(PortalCompanyPositionRole.portalCompanyRoleId, roleId)
					.set(PortalCompanyPositionRole.companyCode, SpringWebUtils.getRequestCompany());
			jdbcDao.insert(positionRole);
		}

		return "success";
	}

}
